import sys 
sys.path.append("core_q1_id")
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler, RobustScaler, Normalizer, StandardScaler
from plotly.offline import download_plotlyjs, init_notebook_mode,  plot
from Surface_Plot  import surface_parameters, surface_parameters_orig
import plotly.graph_objects as go 
import numpy as np

Nt_new = 85
Nr_new = 146
#X=np.loadtxt('Estimated_parameters.txt') 
X = np.loadtxt("./core_q1_id/data_q1_id.txt")
#Scale the points
scaler = MinMaxScaler()
#scaler = RobustScaler()
#scaler = Normalizer()
#scaler = StandardScaler()
scaler.fit(X)
X = scaler.transform(X)
inertia = []

# #Determine the optimal number of clusters
# for i in range(1,11):
#     kmeans = KMeans(n_clusters=i, init="k-means++",n_init=10,tol=1e-04)
#     kmeans.fit(X)
#     inertia.append(kmeans.inertia_)
# fig = go.Figure(data=go.Scatter(x=np.arange(1,11),y=inertia))

# fig.update_layout(title="Inertia vs Cluster Number",xaxis=dict(range=[0,11],title="Cluster Number"),
#                   yaxis={'title':'Inertia'}, annotations=[
#         dict(
#             x=5,
#             y=inertia[4],
#             xref="x",
#             yref="y",
#             text="Elbow! (Optimal number of cluseters)",
#             showarrow=True,
#             arrowhead=7,
#             ax=20,
#             ay=-40
#         )])

# fig.write_image('Elbow.pdf')
# plot(fig)
#Perform the clustering
kmeans = KMeans(
        n_clusters=5, init="k-means++",
        n_init=10,
        tol=1e-08, random_state=10)
kmeans.fit(X)
#Obtain the centers of the clusters
parameters_LSTM_scaled = kmeans.cluster_centers_
parameters_LSTM_unscaled = parameters_LSTM_scaled*(scaler.data_max_ - scaler.data_min_) + scaler.data_min_
#Obtain the clusters
X_clustered = np.zeros((X.shape[0], 7))
X_clustered[:,0:6] = X
X_clustered[:,6] = kmeans.labels_
#Plot the clusters
#shape_1 = 170
#shape_2 = 291
shape_1 = Nt_new
shape_2 = Nr_new
clusters_reshaped = np.reshape(kmeans.labels_, (shape_1, shape_2))
#Change the cluster labels

# for i in range(Nt_new):
#     current_label = clusters_reshaped[i,:]
    
#     for j in range(Nr_new):
#         current_point = current_label[j]
        
#         if current_point == 2:
#             clusters_reshaped[i,j] = 0
#             #current_point = 0
            
#         if current_point == 3:
#             clusters_reshaped[i,j] = 2
#             #current_point = 2
            
#         if current_point == 0:
#             clusters_reshaped[i,j] = 3
#             #current_point = 3
            
#             pass
#         pass
#     pass

for i in range(Nt_new):
    current_label = clusters_reshaped[i,:]
    for j in range(Nr_new):
        current_point = current_label[j]
        
        if current_point == 0:
            clusters_reshaped[i,j] = 3
            #current_point = 1
            
        if current_point == 1:
            clusters_reshaped[i,j] = 0
            #current_point = 3
            
        if current_point == 2:
            clusters_reshaped[i,j] = 1
            #current_point = 4
            
        if current_point == 3:
            clusters_reshaped[i,j] = 2
            #current_point = 2
            
        if current_point == 4:
            clusters_reshaped[i,j] = 4
            #current_point = 0
            pass
        pass
    pass



Tr=surface_parameters_orig(clusters_reshaped,'tr',' ',' ')
# np.savetxt('Clustered_points_new.txt', X_clustered)
# np.savetxt('Parameters_LSTM_new.txt', parameters_LSTM_unscaled)
